package rpg.uiView
{
	import flash.display.Bitmap;
	import flash.display.MovieClip;

	import rpg.Utils.Val;
	import rpg.data.ResData;
	import rpg.manager.UIManager;
	import rpg.resource.IMGResource;
	import rpg.resource.PreLoad;
	import rpg.resource.Resource;
	import rpg.resource.Resources;

	import ui.Component;

	/**
	 * 物品,技能,状态,图标
	 * @author hyy
	 *
	 */
	public class Ico extends Component
	{
		/**
		 * 技能图标
		 */
		public static const ICO_SPELL : int = 1;
		/**
		 * 加载的后缀名
		 * 可以使jpg,png
		 */
		private var loadType : String;
		/**
		 * 图标
		 */
		private var icon : Bitmap;
		/**
		 * ico类型
		 * 可以是装备,武器,道具,技能,或者其他
		 */
		private var icoType : int;

		/**
		 * 加载动画
		 */
		private var loadMc : MovieClip;

		/**
		 * 冷却动画
		 */
		private var coolAnimation : CoolAnimation;

		public function Ico(icoType : int , icoWidth : int = 34 , icoHeight : int = 34 , loadType : String = "jpg")
		{
			super();
			this.loadType = loadType;
			this.setSize(icoWidth , icoHeight);
			this.coolAnimation = new CoolAnimation(icoWidth , icoHeight);
			this.addChild(coolAnimation);
		}

		override public function init() : void
		{
			icon = new Bitmap();
			addChildAt(icon , 0);
		}

		override public function set data(value : Object) : void
		{
			_data = value;
			if(value == null)
				return;
			loadPic(value["icon"]);
		}

		/**
		 * 更新冷却动画
		 * @param value  百分比
		 *
		 */
		public function updateCoolAnimtion(delay : int) : void
		{
			coolAnimation.update(delay)
		}

		public function get isCD() : Boolean
		{
			return coolAnimation.isCD;
		}

		public function createCD(value : int) : void
		{
			coolAnimation.createCD(value);
		}

		/**
		 * 加载图片
		 *
		 */
		public function loadPic(url : int) : void
		{
			Resources.getInstance().preload(PreLoad.resUrl + "ico/Item/" + url + "." + loadType , ResData.IMAGE , Val.LOAD_LVEVEL_ICO , onDownComplete);
			if(loadMc == null)
			{
				loadMc = new UIManager.loadMc();
				loadMc.width = width;
				loadMc.height = height;
			}
			addChild(loadMc);
		}

		private function onDownComplete(res : Resource) : void
		{
			icon.bitmapData = IMGResource(res).bitmapData;
			icon.width = width;
			icon.height = height;
			if(loadMc && loadMc.parent)
				removeChild(loadMc);

		}

	}
}